﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WLive.WLiveObjects
{
    public class User:AGetOp<User>
    {
        public string id;
        public string name;
        public string first_name;
        public string last_name;
        public string link;
        public int birth_day;
        public int birth_month;
        public int birth_year;
        public Work[] work;
        public Email emails;
        public Addr addresses;
        public Phone phones;
        public string locale;
        public DateTime updated_time;

        /// <summary>
        /// Gets folders in root of current user
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Folder> GetFolders()
        {
            try
            {
                return GetFolders(System.IO.SearchOption.TopDirectoryOnly);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Get user by userid
        /// </summary>
        /// <param name="usrid">USER_ID</param>
        /// <returns>Return user object</returns>
        public new static User Get(string usrid)
        {
            try
            {
                if (string.IsNullOrEmpty(usrid))
                    return Get("me");
                return AGetOp<User>.Get(usrid);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Gets folders or subfolders of current user
        /// </summary>
        /// <param name="option">Option if you want to folders with subfolders or not</param>
        /// <returns>List of folders or subfolders of current user</returns>
        public IEnumerable<Folder> GetFolders(System.IO.SearchOption option)
        {
            if (string.IsNullOrEmpty(id))
                id = "me";
            Filter f = new Filter(FilterTypes.FOLDER);
            QueryParameters qp = new QueryParameters();
            qp.Add("filter", f.GetTypesForQuery());
            try
            {
                if (option == System.IO.SearchOption.TopDirectoryOnly)
                    return Requester.Request<Folder>(new RequestObject { url = UrlBuilder1.Build(id + "/skydrive/files", qp) }).ToList();
                else
                {
                    List<Folder> k = new List<Folder>();
                    foreach (Folder folder in Requester.Request<Folder>(new RequestObject { url = UrlBuilder1.Build(id + "/files", qp) }))
                    {
                        k.Add(folder);
                        k.AddRange(folder.GetFolders(option));
                    }
                    return k;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Gets shared folders of current user
        /// </summary>
        /// <returns>List of shared folders of current user</returns>
        public IEnumerable<Folder> GetSharedFolders()
        {
            if (string.IsNullOrEmpty(id))
                id = "me";
            Filter f = new Filter(FilterTypes.FOLDER);
            QueryParameters qp = new QueryParameters();
            qp.Add("filter", f.GetTypesForQuery());
            try
            {
                return Requester.Request<Folder>(new RequestObject { url = UrlBuilder1.Build(id + "/skydrive/shared/files", qp) }).ToList();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Gets album of current user
        /// </summary>
        /// <returns>List of album of current user</returns>
        public IEnumerable<Album> GetAlbums()
        {
            if (string.IsNullOrEmpty(id))
                id = "me";
            Filter f = new Filter(FilterTypes.ALBUM);
            QueryParameters qp = new QueryParameters();
            qp.Add("filter", f.GetTypesForQuery());
            try
            {
                return Requester.Request<Album>(new RequestObject { url = UrlBuilder1.Build(id + "/skydrive/files", qp) }).ToList();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Gets all events of user
        /// </summary>
        /// <param name="usrid">USER_ID</param>
        /// <returns>Events collection of user</returns>
        public static IEnumerable<Event> GetEvents(string usrid)
        {
            if (string.IsNullOrEmpty(usrid))
                usrid = "me";
            try
            {
                return Requester.Request<Event>(new RequestObject { url = UrlBuilder1.Build(usrid) + "/events" }).ToList();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns>Returns friends of current user</returns>
        public IEnumerable<Friend> GetFriends()
        {
            try
            {
                return Requester.Request<Friend>(new RequestObject { url = UrlBuilder1.Build("me/friends") }).ToList();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Create new contact for current user. At least one parameter must be filled.
        /// </summary>
        /// <param name="firstname">Contact firstname</param>
        /// <param name="lastname">Contact lastname</param>
        /// <param name="emails">Contact emails</param>
        /// <param name="works">Contact works</param>
        public void CreateContact(Contact contact, Email emails, Work[] work)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            if (contact != null)
            {
                if (!string.IsNullOrEmpty(contact.first_name))
                    sb.Append("\"first_name\": \"" + contact.first_name + "\"\r\n");
                if(!string.IsNullOrEmpty(contact.last_name))
                    sb.Append("\"last_name\": \"" + contact.last_name + "\"\r\n");
            }
            string email = "";
            if (emails != null)
                email = "\"emails\": " + JSONDataBuilder.Build(emails);
            if (!string.IsNullOrEmpty(email))
                sb.Append(email);
            string wrk = "";
            if (work!=null && work.Length > 0)
            {
                wrk = "\"work\": [ { ";
                for (int i = 0; i < work.Length; i++)
                {
                    wrk += "\"employer\": " + JSONDataBuilder.Build(work[i].employer);
                }
                wrk += "} ]";
            }
            if (!string.IsNullOrEmpty(wrk))
                sb.Append(wrk);
            sb.Append("}");
            try
            {
                RequestObject ro = new RequestObject();
                ro.method = "POST";
                ro.SetData(sb.ToString());
                ro.url = UrlBuilder1.Build(id) + "/contacts";
                Requester.Request<Contact>(ro);
            }
            catch (Exception e)
            { throw e; }
        }
        /// <summary>
        /// Gets all user contacts
        /// </summary>
        /// <returns>Returns current user contacts</returns>
        public IEnumerable<Contact> GetContacts()
        {
            try
            {
                return Requester.Request<Contact>(new RequestObject { url = UrlBuilder1.Build(id) + "/contacts" }).ToList();
            }
            catch (Exception e)
            { throw e; }
        }

    }
}
